/**
 * Copyright (C), 2020, Y2 T205
 * FileName: SeckillSentinelConfig
 * Author: Lenovo
 * Date: 2020-06-16 15:13
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */


package com.atguigu.gulimall.seckill.config;


import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.R;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Lenovo
 * @create 2020-06-16
 * @since 1.0.0
 */
@Configuration
public class SeckillSentinelConfig {

   public SeckillSentinelConfig(){
       WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
           @Override
           public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException {
               response.setCharacterEncoding("utf-8");
               response.setContentType("application/json");
               R error = R.error(BizCodeEnume.TO_MANY_REQUEST.getCode(), BizCodeEnume.TO_MANY_REQUEST.getMsg());
               response.getWriter().write(JSON.toJSONString(error));
           }
       });
    }

}
